<?
class UserController extends PrincipioLab_Controller_Action
{
    const ACTION_VIEW = 0;
    const ACTION_ADD = 1;
    const ACTION_EDIT = 2;
    const ACTION_DELETE = 3;


    private function setActionViewData($action)
    {
        switch ($action)
        {
            case self::ACTION_ADD:
                Principio_Common::setButtons(
                    "Add user",
                    "ajaxUsers({id: -1, action: 'add', use_inner: true});",
                    "addItem({menu: 'users', container: 'inner'});"
                );
                break;

            case self::ACTION_EDIT:
                $model_action_menu = new Principio_Model_ActionMenu();
                $model_action_menu->setJsAjaxFunction("Users")->setItemID($this->view->model_user->getID())->
                    setMenu(Principio_Model_MenuItem::fromArray(['Edit' => "edit", 'Delete' => "delete"]));

                $this->view->model_action_menu = $model_action_menu;

                Principio_Common::setButtons(
                    "Apply",
                    "ajaxUsers({action: 'edit', id: ".$this->view->model_user->getID()."});",
                    "ajaxUsers({id: ".$this->view->model_user->getID().", action: 'edit', no_apply: true});"
                );
                break;
        }
    }


    private function validate($check_password = false)
    {
        if ($this->checkPostItemString("login", "Login"))
            $this->view->model_user->setLogin($this->post_data['login']);
        if ($check_password)
        {
            if ($this->checkPostItemString("password", "Password"))
                $this->view->model_user->setPassword($this->post_data['password']);
        }
        if (array_key_exists($this->post_data['role'], $this->view->model_user->getRoles()))
            $this->view->model_user->setRole($this->post_data['role']);
        if (!Principio_Common::$is_error)
        {
            Principio_Common::checkValidity(Principio_Model_UserMapper::checkUniqueLogin($this->view->model_user), "User with login [".$this->view->model_user->getLogin()."] already exists.");
        };

        return !Principio_Common::$is_error;
    }


    public function indexAction()
    {
        $this->view->title = "Users";
    }


    public function viewAction()
    {
        return $this->editAction(self::ACTION_VIEW);
    }


    public function addAction()
    {
        if ($this->is_post)
        {
            $this->view->title = "Add new user";
            $this->setActionViewData(self::ACTION_ADD);

            if ($this->is_apply)
            {
                $this->view->model_user = new Principio_Model_User();
                $this->view->model_user
                    ->setName($this->post_data['name'])
                    ->setLastName($this->post_data['last_name'])
                    ->setRegisterDate(date("Y-m-d H:i:s"))
                    ->setLastLogin(null);
                if ($this->validate(true))
                {
                    if ($this->view->model_user->add())
                    {
                        Principio_Common::reportMessage("User was added.");
                        $this->view->action = "view";
                        $this->view->title = "View and Edit user [".$this->view->model_user->getLogin()."]";
                        $this->setActionViewData(self::ACTION_EDIT);
                    }
                };
            }
            else
                $this->view->model_user = new Principio_Model_User();
        }
    }


    public function editAction($action = self::ACTION_EDIT)
    {
        if ($this->is_post)
        {
            $this->view->action = ($action == self::ACTION_EDIT) ? "edit" : "view";
            $this->view->model_user = Principio_Model_UserMapper::fetch($this->post_data['id']);
            $model_user_old = clone $this->view->model_user;

            //$form_users = new Principio_Form_Users();

            if (($this->is_apply) && ($this->validate()))
            {
                $this->view->model_user->setName($this->post_data["name"])->setLastName($this->post_data["last_name"]);

                if (!Principio_Common::$is_error)
                {
                    if ($this->view->model_user->save())
                        Principio_Common::reportMessage("User was updated.");
                };
            }
        }

        $this->view->title = "View and Edit user [".(Principio_Common::$is_error ? $model_user_old->getLogin() : $this->view->model_user->getLogin())."]";
        $this->setActionViewData(self::ACTION_EDIT);
    }


    public function deleteAction()
    {
        if ($this->is_post)
        {
            if ($this->checkPostItemInt("id", "User ID"))
            {
                $model_user = Principio_Model_UserMapper::fetch($this->post_data["id"]);
                if ($model_user->delete())
                {
                    Principio_Common::reportMessage("User was deleted.");
                };
            };
        }
    }
}
?>
